package com.zhdl.hardware.clamping_jaw.inspire.service.impl;

import com.project.common.util.ByteHelper;
import com.zhdl.hardware.clamping_jaw.inspire.repository.InspirePortCmdHandleDao;
import com.zhdl.hardware.clamping_jaw.inspire.service.InspireSendCmdService;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class InspireSendCmdServiceImpl implements InspireSendCmdService {

    /* 读取驱动器位置 H0021 HFFEC~H0834 */
    String cmd1 = "01 03 00 21 00 01 D4 00 ";
    /* 写入驱动器位置 H0020 H0000~H07D0 */
    String cmd2 = "01 06 00 20 07 D0 8B AC ";
    /* 读取驱动器写入位置 */
    String cmd3 = "01 03 00 20 00 01 85 C0 ";

    @Resource
    private InspirePortCmdHandleDao inspirePortCmdHandleDao;

    /**
     * 电机通用调试
     *
     * @param port 串口
     * @param instruct 指令编号
     * @param callback 回调
     */
    @Override
    public void inspireGenericCmd(String port, int instruct, CommonCallback<String> callback){
        String cmd = "";
        switch (instruct){
            case 1 ->{
                cmd = cmd1;
            }
            case 2 ->{
                cmd = cmd2;
            }
            case 3 ->{
                cmd = cmd3;
            }
        }
        inspirePortCmdHandleDao.inspireGenericCmd(port, cmd, bytes -> {
            String hexResult = ByteHelper.bytesArrayToHexString(bytes);
            log.info("当前返回结果为" + hexResult);
            callback.statusCallback(hexResult);
        });
    }
}
